<template>
  <div class="wrapper wrapper-content animated fadeInRight v-cloak" v-cloak>
    <div class="row">
      <div class="col-md-12">
        <div v-if="fileType=='pdf'">
          <pdf :src="fileSrc"
               v-for="i in numPages"
               :key="i"
               :page="i"
          ></pdf>
        </div>
        <div v-else>
          只支持pdf文档在线查看,<a target="_blank" :href="item.file">其它格式({{fileType}})请点击下载</a>
        </div>
      </div>
    </div>
  </div>
</template>
<script type="text/javascript">
  import '@/assets/css/style.css'
  import pdf from 'vue-pdf'
  import axios from 'axios';
  import {apiUtil, site} from '@/assets/js/boss'
  import Swal from 'sweetalert2'

  export default {
    components: {pdf},
    data() {
      return {
        item: {},
        fileType: 'pdf', // 文件类型
        fileSrc: '',
        numPages: 0
      }
    },
    mounted() {
      let that = window.$vueApp = this;
      document.getElementsByTagName('body')[0].className = 'fixed-sidebar full-height-layout';
      that.staff.init(function () {
        if (apiUtil.existSessionKey(site.reportTask.itemAudit)) {
          that.info();
        }
      });
    },
    methods: {
      info() {
        let that = this;
        axios.get(site.reportTaskSubmit.info + sessionStorage.getItem(site.reportTask.itemAudit), {}).then(function (response) {
          const result = response.data;
          if (result.code === 0) {
            let item = that.item = result.data;
            if (apiUtil.notEmpty(item.file)) {
              // 获取扩展名
              that.fileType = item.file.substr(item.file.lastIndexOf('.') + 1, item.file.length);
              if (that.fileType === 'pdf') {
                let loadingTask = pdf.createLoadingTask(item.file);
                loadingTask.promise.then(pdf => {
                  that.fileSrc = loadingTask
                  that.numPages = pdf.numPages
                }).catch((err) => {
                  Swal.fire('pdf加载失败' + err)
                })
              }
            }
          } else {
            Swal.fire(result.message);
          }
        });
      }
    }
  }
</script>
